<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash

use App\Response\Response;

class MerchantController extends BaseController
{

    //商户列表
    public function merchantList(Request $request)
    {
        $filter = $request->only(['merchant_type', 'status', 'name']);
        $list = app('repo_merchant')->MerchantList($filter);

        return Response::sendData($list);
    }

    //添加
    public function merchantAdd(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'name'          => 'required|string',
                'pay_config'    => 'required|min:5',
                'desc'          => 'required',
                'merchant_type' => 'required|integer|in:1,2',
                'status'        => 'required|integer|in:0,1',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        //判断是否为json 格式
        $is_json = $this->isJson($request->input('pay_config'));
        if ($is_json == false) {
            throw new BasicException(10001, '配置文件不是json格式');
        }

        //数据组合
        $param['name'] = $request->input('name');
        $param['pay_config'] = $request->input('pay_config');
        $param['desc'] = $request->input('desc');
        $param['merchant_type'] = $request->input('merchant_type');
        $param['status'] = $request->input('status');
        $param['add_time'] = time();
        $param['update_time'] = time();
        //执行添加操作
        app('repo_merchant')->merchantAdd($param);

        return Response::sendData(true);
    }

    //修改
    public function merchantSave(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id'            => 'required',
                'name'          => 'required|string',
                'pay_config'    => 'required|min:5',
                'desc'          => 'required|string',
                'merchant_type' => 'required|integer|in:1,2',
                'status'        => 'required|integer|in:0,1',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        //判断是否为json 格式
        $is_json = $this->isJson($request->input('pay_config'));
        if ($is_json == false) {
            throw new BasicException(10001, '配置文件不是json格式');
        }

        //数据组合
        $param['id'] = $request->input('id');
        $param['name'] = $request->input('name');
        $param['pay_config'] = $request->input('pay_config');
        $param['desc'] = $request->input('desc');
        $param['merchant_type'] = $request->input('merchant_type');
        $param['status'] = $request->input('status');
        $param['update_time'] = time();

        //执行编辑操作
        app('repo_merchant')->merchantSave($param);

        return Response::sendData(true);
    }

    //判断是否为json
    public function isJson($string)
    {
        json_decode($string);

        return (json_last_error() == JSON_ERROR_NONE);
    }


}
